/*
 * Classname SessionTest.java
 * 
 * Version Information
 * Date Feb 24, 2006
 *
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.hibernate.tests;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import net.amsoft.iservice.isso.hibernate.Cookie;
import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.Response;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOHelpers;
import net.amsoft.iservice.isso.util.Messages;
import net.amsoft.iservice.util.exception.IServiceException;

/**
 * @author jyoti
 * 
 *  
 */

public class SessionTest extends TestCase
{
    private static long pId = 1;

    public static void main(String[] oArgs) {
        TestRunner.run(suite());
    } // main()

    public static Test suite() {
        return new TestSuite(IBrokerManagerTest.class);

    } // suite()

    protected void setUp() throws Exception {

        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    private ResultSet getDataFromDB(String sql, Connection oCon) {
        String strSQLGet = sql;

        try {
            PreparedStatement psGet = oCon.prepareStatement(strSQLGet);
            ResultSet oRs = psGet.executeQuery();
            return oRs;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }

    public Connection getConnection() {
        Connection oCon = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String DB_URL = "jdbc:mysql://localhost:3306/hibernate";
            oCon = DriverManager.getConnection(DB_URL, "root", "root");
            oCon.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println("Exception :" + e.getMessage());
        }
        return oCon;
    }
    private void cleanMachineCookieTable() throws IOException, SQLException {
        String strDelete = " DELETE FROM COOKIE WHERE  PRINCIPAL_ID ="
                + Integer.parseInt(Messages.getString("session.PrincipalId"));
        Connection oCon = null;
        try {
            oCon = getConnection();
            oCon.prepareStatement(strDelete).execute();
            oCon.commit();

        } catch (SQLException oSQEx) {
            oSQEx.printStackTrace();
        } finally {
            oCon.close();
        }
    }

    private void cleanResponseTable() throws IOException, SQLException {
        String strDelete = " DELETE FROM PENDING_RESPONSE WHERE "
                + " ENC_ARTIFACT_ID = '"
                + Messages.getString("session.ArtifactId") + "'";
        Connection oCon = null;
        try {
            oCon = getConnection();
            oCon.prepareStatement(strDelete).execute();
            oCon.commit();

        } catch (SQLException oSQEx) {
            oSQEx.printStackTrace();
        } finally {
            oCon.close();
        }
    }

    private void cleanSessionTable() throws IOException, SQLException {
        cleanResponseTable();

        String strDelete = " DELETE FROM SESSION WHERE  PRINCIPAL_ID ="
                + Integer.parseInt(Messages.getString("session.PrincipalId"));
        Connection oCon = null;
        try {
            oCon = getConnection();
            oCon.prepareStatement(strDelete).execute();
            oCon.commit();

        } catch (SQLException oSQEx) {
            oSQEx.printStackTrace();
        } finally {
            oCon.close();
        }
    }

    public void testCreateSession() throws IServiceException, IOException,
            SQLException {
        PrincipalTest.makePrincipal();

        Cookie oCookie = new Cookie();
        oCookie.setEncCookieName(Messages.getString("session.SesionToken"));
        oCookie.setState((short) ISSOConstants.STATE_ACTIVE);
        oCookie.setTypeFlag(0);
        if (pId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(pId));
            oCookie.setPrincipal(oIB);
        }

        oCookie.insert();
        Cookie.listAll();
        Connection oCon = null;
        try {

            oCon = getConnection();
            String sqlGet = "SELECT ENC_COOKIE_NAME FROM COOKIE WHERE "
                    + "PRINCIPAL_ID = "
                    + Integer.parseInt(Messages
                            .getString("session.PrincipalId"))
                    + " AND ENC_COOKIE_NAME  ='"
                    + Messages.getString("session.SesionToken") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(Messages.getString("session.SesionToken"), rs
                    .getString(1));

        } finally {
            oCon.close();
        }

    }

    public void testCreateMachineCookie() throws IServiceException,
            IOException, SQLException {
        cleanMachineCookieTable();

        PrincipalTest.makePrincipal();

        Cookie oCookie = new Cookie();
        oCookie.setEncCookieName(Messages.getString("session.SesionToken") + 1);
        oCookie.setState((short) ISSOConstants.STATE_ACTIVE);
        oCookie.setTypeFlag(1);
        if (pId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(pId));
            oCookie.setPrincipal(oIB);
        }

        oCookie.insert();
        Cookie.listAll();
        Connection oCon = null;
        try {

            oCon = getConnection();
            String sqlGet = "SELECT ENC_COOKIE_NAME FROM COOKIE WHERE "
                    + "PRINCIPAL_ID = "
                    + Integer.parseInt(Messages
                            .getString("session.PrincipalId"))
                    + " AND ENC_COOKIE_NAME  ='"
                    + Messages.getString("session.SesionToken") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(Messages.getString("session.SesionToken"), rs
                    .getString(1));

        } finally {
            oCon.close();
        }

    }

    public void testCreateResponse() throws IServiceException, SQLException,
            IOException {

        cleanResponseTable();
        int cookieId = 1;
        Response oResponse = new Response();
        oResponse.setEncArtifactId(Messages.getString("session.ArtifactId"));
        oResponse.setResponse(Messages.getString("session.Response"));
        oResponse.setState((short) 1);

        Cookie oCookie = (Cookie) ISSOHelpers.fetch(Cookie.class, new Long(
                cookieId));
        oResponse.setCookie(oCookie);

        Connection oCon = null;
        try {

            oResponse.insert();

            oCon = getConnection();
            String sqlGet = "SELECT ENC_ARTIFACT_ID FROM " + "RESPONSE WHERE "
                    + "' ENC_ARTIFACT_ID ='"
                    + Messages.getString("session.ArtifactId") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(Messages.getString("session.ArtifactId"), rs
                    .getString(1));

        } catch (IServiceException e) {
            fail("Create does not work correctly");
        } finally {
            oCon.close();
        }
    }

    public void updateSessionCookie() throws IServiceException, SQLException {
        Cookie oCookie = (Cookie) ISSOHelpers.fetchCookieByName(Messages
                .getString("session.SesionToken"), 0);
        oCookie.setState((short) 3);

        Connection oCon = null;
        try {
            oCon = getConnection();
            oCookie.update();

            String sqlGet = "SELECT ENC_ARTIFACT_ID FROM " + "RESPONSE WHERE "
                    + "' ENC_ARTIFACT_ID ='"
                    + Messages.getString("session.ArtifactId") + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(Messages.getString("session.ArtifactId"), rs
                    .getString(1));

        } finally {
            oCon.close();
        }

    }

    private void updateMachineCookie() throws IServiceException, SQLException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName(Messages
                .getString("session.SesionToken") + 1, 1);
        oCookie.setState((short) 3);
        Connection oCon = null;
        try {
            oCon = getConnection();
            oCookie.update();

            String sqlGet = "SELECT ENC_COOKIE_NAME  FROM " + "COOKIE WHERE "
                    + "'  ENC_COOKIE_NAME ='"
                    + (Messages.getString("session.SesionToken") + 1) + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals((Messages.getString("session.SesionToken") + 1), rs
                    .getString(1));

        } finally {
            oCon.close();
        }

    }

    private void removeSessionCookie() throws IServiceException, SQLException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName(Messages
                .getString("session.SesionToken"), 1);

        Connection oCon = null;
        try {
            oCon = getConnection();
            oCookie.remove();

            String sqlGet = "SELECT STATE_ID  FROM " + "COOKIE WHERE "
                    + "'  ENC_COOKIE_NAME ='"
                    + (Messages.getString("session.SesionToken")) + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(ISSOConstants.STATE_INACTIVE, rs.getShort(1));

        } finally {
            oCon.close();
        }

    }
    private void removeMachineCookie() throws IServiceException, SQLException {
        Cookie oCookie = ISSOHelpers.fetchCookieByName(Messages
                .getString("session.SesionToken") + 1, 1);

        Connection oCon = null;
        try {
            oCon = getConnection();
            oCookie.remove();

            String sqlGet = "SELECT STATE_ID  FROM " + "COOKIE WHERE "
                    + "'  ENC_COOKIE_NAME ='"
                    + (Messages.getString("session.SesionToken")) + "'";

            ResultSet rs = getDataFromDB(sqlGet, oCon);
            rs.next();
            assertEquals(ISSOConstants.STATE_INACTIVE, rs.getShort(1));

        } finally {
            oCon.close();
        }
    }

}